package com.aitaoxue.palx.widget;


import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.TextView;

import com.aitaoxue.palx.R;

public class DateChooseDialog extends AlertDialog implements OnClickListener,
		OnDateChangedListener {

	private TextView title;
	private DatePicker picker;

	private OnDateSetListener listener;

	public interface OnDateSetListener {
		void onDateSet(DatePicker picker, int year, int month, int day);
	}

	public DateChooseDialog(Context context, int theme, int year, int month,
			int day, String type, OnDateSetListener listener) {
		super(context, theme);

		setButton(BUTTON_NEUTRAL, "不限", this);
		setButton(BUTTON_POSITIVE, "确定", this);
		setButton(BUTTON_NEGATIVE, "取消", this);

		this.listener = listener;

		setIcon(0);

		LayoutInflater inflater = LayoutInflater.from(context);
		View view = inflater.inflate(R.layout.date_choose, null);
		setView(view);

		title = (TextView) view.findViewById(R.id.title);
		picker = (DatePicker) view.findViewById(R.id.date);
		
		if (type.equals("start")) {
			title.setText("开始时间");
		} else {
			title.setText("结束时间");
		}

		picker.init(year, month, day, this);

	}

	@Override
	public void onClick(DialogInterface dialog, int which) {
		switch (which) {
		// 不限
		case BUTTON_NEUTRAL:
			listener.onDateSet(null, 0, 0, 0);
			break;
		// 确定
		case BUTTON_POSITIVE:
			listener.onDateSet(picker, picker.getYear(), picker.getMonth()+1,
					picker.getDayOfMonth());
			break;
		default:
			break;
		}
	}

	@Override
	public void onDateChanged(DatePicker view, int year, int monthOfYear,
			int dayOfMonth) {
		picker.init(year, monthOfYear, dayOfMonth, this);
	}

}
